﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using METU_CMT.Business;

namespace METU_CMT
{
    public partial class InstructorEditScheduleScreen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                BindScheduleInfo( );
            }
        }

        private void BindScheduleInfo()
        {
            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);
            dtgSchedule.DataSource = _currentCourse.Schedule;
            dtgSchedule.DataBind();
        }

        protected void OnBtnAddClicked(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(txtDate.Text) == true)
            {
                return;    
            }

            if (string.IsNullOrEmpty(txtTime.Text) == true)
            {
                return;
            }

            if (string.IsNullOrEmpty(txtDescription.Text) == true)
            {
                return;
            }

            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);
            Event _event = new Event(txtDate.Text, txtTime.Text, txtDescription.Text, chkSubmission.Checked);
            _currentCourse.AddEvent(_event);

            NotifyUsers(txtDate.Text, txtTime.Text, txtDescription.Text, chkSubmission.Checked);

            BindScheduleInfo();
        }

        private void NotifyUsers(string date, string time, string description, bool isChecked)
        {
            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);

            string _body = "<b>Course Name: </b>" + _currentCourse.CourseName + "<br/><b>Event Description: </b>" +
                           description+"<br/><b>Due Date: </b>"+date+ " "+time+"<br/><b>Submission Required: </b>"+isChecked;
            string _mailSubject = "New Event is added!";

            List<Student> _students = _currentCourse.Students;
            string _mailAdresses = string.Empty;

            foreach (var _student in _students)
            {
                if (!string.IsNullOrEmpty(_student.Email) && _student.Email.Contains("@"))
                {
                    _mailAdresses += _student.Email + ";";
                }
            }

            Email _email = new Email(_mailSubject, _body, _mailAdresses);
            _email.SendEmail();
        }

        protected void OnLnkBtnAssignTeams(object sender, EventArgs e)
        {
            Response.Redirect("~/AssignReviewTeamScreen.aspx");
        }

        protected void OnDtgScheduleCancellingEdit( object sender, GridViewCancelEditEventArgs e )
        {
            dtgSchedule.EditIndex = -1;
            BindScheduleInfo( );
        }

        protected void OnDtgScheduleRowEditing( object sender, GridViewEditEventArgs e )
        {
            dtgSchedule.EditIndex = e.NewEditIndex;
            BindScheduleInfo( );
        }

        protected void OnDtgScheduleRowCommand( object sender, GridViewCommandEventArgs e )
        {

        }

        protected void OnDtgScheduleRowUpdating( object sender, GridViewUpdateEventArgs e )
        {
            dtgSchedule.Rows[e.RowIndex].Focus();
            int _eventId = CommonMethods.GetIdFromGridViewRow(dtgSchedule.Rows[e.RowIndex]);

            TextBox _txtDueDateEdit = (TextBox)dtgSchedule.Rows[e.RowIndex].FindControl("txtDueDateEdit");
            TextBox _txtDueTimeEdit = (TextBox)dtgSchedule.Rows[e.RowIndex].FindControl("txtDueTimeEdit");
            TextBox _txtDescriptionEdit = (TextBox)dtgSchedule.Rows[e.RowIndex].FindControl("txtDescriptionEdit");
            CheckBox _chkSubmissionExistEdit = (CheckBox)dtgSchedule.Rows[e.RowIndex].FindControl("chkSubmissionExistEdit");

            Event _event = new Event( _txtDueDateEdit.Text, _txtDueTimeEdit.Text, _txtDescriptionEdit.Text,
                                      _chkSubmissionExistEdit.Checked );

            CourseInstructor _currentCourse = ( CourseInstructor ) EntityFactory.GetCurrentCourse( UserType.Instructor );


            _currentCourse.UpdateEvent( _eventId, _event );

            dtgSchedule.EditIndex = -1;

            BindScheduleInfo( );
        }

        protected void OnDtgScheduleRowDeleting( object sender, GridViewDeleteEventArgs e )
        {
            int _eventId = CommonMethods.GetIdFromGridViewRow( dtgSchedule.Rows[ e.RowIndex ] );
            CourseInstructor _currentCourse = (CourseInstructor)EntityFactory.GetCurrentCourse(UserType.Instructor);

            _currentCourse.DeleteEvent( _eventId );

            BindScheduleInfo( );

        }
    }
}